package xin.xihc.jba.sql.clause;

import xin.xihc.jba.sql.KV;

import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;

/**
 * NOT IN 条件
 *
 * @author Leo.Xi
 * @date 2020/3/5
 * @since 1.0
 **/
public class NotIn extends Clause<Collection<?>> {

    public NotIn(String columnName, Collection<?> value) {
        super(columnName, value);
    }

    @Override
    public Collection<?> value() {
        if (this.value != null) {
            Iterator<?> iterator = this.value.iterator();
            LinkedList<Object> temp = new LinkedList<>();
            while (iterator.hasNext()) {
                Object next = iterator.next();
                if (next != null && next.getClass().isEnum()) {// 处理枚举
                    temp.add(next.toString());
                } else {
                    temp.add(next);
                }
            }
            return temp;
        }
        return null;
    }

    @Override
    public String operation() {
        return " NOT IN ";
    }

    @Override
    public String toSql(KV kv) {
        String key = kv.add(this.value());
        return this.columnName + this.operation() + "(:" + key + ")";
    }

}
